package com.quanquan.user.repository;

import com.quanquan.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 功能描述:user  Dao层
 *
 * @ClassName: UserRepository
 * @Author: xiaoqianh丶
 * @Date: 2019-03-11 15:08
 * @Version: V1.0
 */
@Repository
public interface UserRepository extends JpaRepository<User, String>, JpaSpecificationExecutor<User> {


    /**
     * 根据手机号查询用户
     *
     * @param phone
     * @param num
     * @return
     */
    List<User> findUserByPhoneAndRemove(String phone, Integer num);

    /**
     * 根据卡号号查询用户
     *
     * @param memberCardNum
     * @param num
     * @return
     */
    List<User> findUserByMemberCardNumAndRemove(String memberCardNum, Integer num);


    /**
     * 根据用户的id查询用户信息
     *
     * @param id
     * @param num
     * @return
     */
    List<User> findUserByIdAndRemove(String id, Integer num);

    /**
     * 按创建时间排序并且未删除
     *
     * @param
     * @param num
     */
    List<User> findAllByRemoveOrderByCreateTimeDesc(Integer num);



    User findByPhoneAndRemove(String phone ,Integer num);

    @Query(value = "SELECT * FROM oo_user where status_remove = 0",nativeQuery = true)
    List<User> findAllUser();


    User findByWxOpenidAndFreeze(String openid, Integer num);


    @Query(value = "SELECT * FROM oo_user where freeze = ?2 and wx_open_id = ?1",nativeQuery = true)
    List<User> findByWxOpenid(String openid, Integer num);

    @Query(value = "SELECT * FROM oo_user where freeze = ?2 and parking_open_id = ?1",nativeQuery = true)
    List<User> findByParkingOpenId(String openid, Integer num);

    List<User> findByOpenidAndFreeze(String openid, Integer num);

    @Modifying
    @Transactional
    @Query(value = "update oo_user set update_time = now() where open_id = ?1",nativeQuery = true)
    void updateUserUpdateTime(String openid);

}
